package com.hexacta.hrs.sandbox.pr.data;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hexacta.hrs.sandbox.pr.model.Person;
import com.hexacta.hrs.sandbox.pr.model.Profile;
import com.hexacta.hrs.sandbox.pr.model.Project;
import com.hexacta.hrs.sandbox.pr.model.Review;

public class DaoImpl extends HibernateDaoSupport implements Dao {
	
	public Long add(PersistentObject po) {
		this.getHibernateTemplate().save(po);
		return po.getId();
	}
	
	public void update(PersistentObject po) {
		this.getHibernateTemplate().merge(po);
	}

	@SuppressWarnings("unchecked")
	public List<? extends PersistentObject> findByName(Class<? extends PersistentObject> po, String name) {
		List<?> list = getHibernateTemplate().find("from " + po.getName() + " as p where p.name = '" + name + "'");
		
		//TODO esto es horrible, se hace por el bug de hibernate de
		//lazy ini
        if( po.equals(Person.class) ) {
        	List<Person> persons = (List<Person>) list;
    		for( Person person : persons ) {
    			for( Profile profile : person.getProfiles() ) {
    				profile.getName();
    			}
    		}
        } else if( po.equals(Project.class)) {
        	List<Project> projects = (List<Project>) list;
        	for( Project project : projects ) {
        		project.getLeader();
        		//TODO levantar las reviews?
        	}
        }  else if( po.equals(Review.class)) {
        	List<Review> reviews = (List<Review>) list;
        	for( Review review : reviews ) {
        		for( Person person : review.getAuthors() )
        			person.getName();
        		for( Person person : review.getRevisors() )
        			person.getName();
        		
        		review.getCategory().getName();
        		if( review.getMod() != null )
        			review.getMod().getName();
        	}
        } 
        return (List<? extends PersistentObject>) list;
	}

	@SuppressWarnings("unchecked")
	public List<? extends PersistentObject> listAll(Class<? extends PersistentObject> po) {
		List<? extends PersistentObject> list = getHibernateTemplate().find("from " + po.getName());

		
		//TODO esto es horrible, se hace por el bug de hibernate de
		//lazy ini
        if( po.equals(Person.class) ) {
        	List<Person> persons = (List<Person>) list;
    		for( Person person : persons ) {
    			for( Profile profile : person.getProfiles() ) {
    				profile.getName();
    			}
    		}
        } else if( po.equals(Project.class)) {
        	List<Project> projects = (List<Project>) list;
        	for( Project project : projects ) {
        		project.getLeader();
        		//TODO levantar las reviews?
        	}
        }  else if( po.equals(Review.class)) {
        	List<Review> reviews = (List<Review>) list;
        	for( Review review : reviews ) {
        		for( Person person : review.getAuthors() )
        			person.getName();
        		for( Person person : review.getRevisors() )
        			person.getName();
        		
        		review.getCategory().getName();
        		if( review.getMod() != null )
        			review.getMod().getName();
        	}
        } 

        return list;
	}

	public void remove(PersistentObject po) {
		this.getHibernateTemplate().delete(po);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<? extends PersistentObject> findById(Class<? extends PersistentObject> po, Long id) {
		List<?> list = getHibernateTemplate().find("from " + po.getName() + " as p where p.id = '" + id.toString() + "'");
		
		//TODO esto es horrible, se hace por el bug de hibernate de
		//lazy ini
        if( po.equals(Person.class) ) {
        	List<Person> persons = (List<Person>) list;
    		for( Person person : persons ) {
    			for( Profile profile : person.getProfiles() ) {
    				profile.getName();
    			}
    		}
        } else if( po.equals(Project.class)) {
        	List<Project> projects = (List<Project>) list;
        	for( Project project : projects ) {
        		project.getLeader();
        		//TODO levantar las reviews?
        	}
        }  else if( po.equals(Review.class)) {
        	List<Review> reviews = (List<Review>) list;
        	for( Review review : reviews ) {
        		for( Person person : review.getAuthors() )
        			person.getName();
        		for( Person person : review.getRevisors() )
        			person.getName();
        		
        		review.getCategory().getName();
        		if( review.getMod() != null )
        			review.getMod().getName();
        	}
        } 
		return (List<? extends PersistentObject>) list;
	}
	
}